<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Admin\BaseController;
use App\Repositories\UserRepositoryEloquent;

use App\User;
use App\Models\Recharge;
use Breadcrumbs, Toastr,Config,Validator;
use Illuminate\Http\Request;
use App\Models\FinancialDetails;

class RechargeController extends BaseController{


    public function index(Request $request){
        $where=[];
        $whereHas=[];
        $data=['status'=>-1,'name'=>'','type'=>'','mobile'=>'','from'=>'','to'=>''];

        if(!empty($request->all())){
            //if($request->name){
            //dd($request->all());
            $datas = $request->all();
            foreach($datas as $k=>$v){
                if(($k=='name' || $k=='mobile') && $v!=''){
                    $whereHas[] = ["$k",'like','%'.$v.'%'];
                }
                if($k=='from' && $v!=''){
                    //$where[] = ['created_at','>',strtotime($v)];
                    $where[] = ['created_at','>',$v];
                }
                if($k=='to' && $v!=''){
                    //$where[] = ["created_at",'<',strtotime($v)];
                    $where[] = ["created_at",'<',$v];
                }
                if($k=='status' && $v!='' && $v!=-1){
                    $where[] = ["$k",'=',$v];
                }
                $data[$k] =$v;
                
            }
            //dd($where);
            //$users = User::where($where)->paginate(20);
        }

        $recharges = Recharge::where($where)
                        ->whereHas('user',function($query) use($whereHas){
                            $query->where($whereHas);
                        })->orderBy('id','desc')->paginate(20);
        $total_money = Recharge::where($where)
                        ->whereHas('user',function($query) use($whereHas){
                            $query->where($whereHas);
                        })->sum('money');
                        //var_dump($where);
                        //echo $total_money;
        return view('admin.finance.recharge',compact('recharges','data','total_money'));
    }

}